package com.noble.meether.ui.setting;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

import com.noble.meether.R;
import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.glamour.module.logic.baseui.custom.SimpleTipDialog;
import com.noble.glamour.module.logic.config.Constant;
import com.noble.glamour.module.util.PickerDialogUtil;
import com.noble.glamour.module.util.UIShow;
import com.noble.library.base.BaseAppCompatActivity;
import com.noble.library.log.PSP;
import com.noble.library.utils.APKUtil;
import com.suke.widget.SwitchButton;

/**
 * 设置页面
 */
public class SettingAct extends BaseAppCompatActivity implements OnClickListener {
    private SwitchButton setting_msg;
    private Boolean Message_Status;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings_layout);
        setBackView(getResources().getString(R.string.title_set));
        initView();
    }

    private void initView() {
        findViewById(R.id.delet_friend).setOnClickListener(this);
        findViewById(R.id.setting_logoff).setOnClickListener(this);
        findViewById(R.id.setting_about).setOnClickListener(this);
        findViewById(R.id.app_market).setOnClickListener(this);

        initSwitchButton();
    }

    private void initSwitchButton() {
        setting_msg = findViewById(R.id.setting_msg);

        // 是否新消息提醒
        Message_Status = PSP.getInstance().getBoolean(Constant.SETTING_MESSAGE, Constant.SETTING_MESSAGE_DEFAULT);
        setting_msg.setChecked(Message_Status);
        onClickSwitchBtn();
    }

    /**
     * 开关状态切换
     */
    private void onClickSwitchBtn() {
        // 新消息提醒
        setting_msg.setOnCheckedChangeListener((view, isChecked) -> switchMsg());
    }

    private void switchMsg() {
        if (Message_Status) {
            Message_Status = false;
            PSP.getInstance().put(Constant.SETTING_MESSAGE, Message_Status);
        } else {
            Message_Status = true;
            PSP.getInstance().put(Constant.SETTING_MESSAGE, Message_Status);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.delet_friend:
                UIShow.showMyDefriends(this);
                break;

            case R.id.setting_logoff:// 退出登录
                PickerDialogUtil.showSimpleTipDialog(this, new SimpleTipDialog.ConfirmListener() {
                    @Override
                    public void onCancel() {
                    }

                    @Override
                    public void onSubmit() {
                        exitLogin();
                    }
                }, getResources().getString(R.string.dal_exit_content), getResources().getString(R.string.dal_exit_title), getResources().getString(R.string.cancel), getResources().getString(R.string.ok), true);
                break;
            case R.id.setting_about:
                UIShow.showAboutAct(this);
                break;
            case R.id.app_market:
                APKUtil.skipMarket(this, ModuleMgr.getAppMgr().getPackageName());
                break;
        }
    }

    public void exitLogin() {
        ModuleMgr.getLoginMgr().logout();
        ModuleMgr.getCenterMgr().clearUserInfo();
        setResult(200);
        finish();
    }
}